package concurrent1;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo1 {

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        
        new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName()+" thread lock");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            lock.unlock();
            System.out.println(Thread.currentThread().getName()+" thread unlock");
        },"thread-1").start();
        
       Thread.sleep(500);

       new Thread(() -> {
           lock.lock();
           System.out.println(Thread.currentThread().getName()+" thread lock");
           lock.unlock();
       },"thread2").start();
        new Thread(() -> {
           if( lock.tryLock()){
               System.out.println(Thread.currentThread().getName()+" geted lock");
           }else{
               System.out.println(Thread.currentThread().getName()+" unget lock");
               System.out.println(lock.isLocked());
               System.out.println(lock.getQueueLength());
           }
        },"thread3").start();
    }
}
